/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-07-17
 * Time: 22:51
 */
public class Test {
    //1.BM15删除有序链表中的重复的元素|（双指针）
    public ListNode deleteDuplicates (ListNode head) {
        if(head == null) {
            return null;
        }
        ListNode cur = head.next;
        ListNode prve = head;
        while(cur != null) {
            if(cur.val != prve.val) {
                prve = cur;
                cur = cur.next;
            } else {
                cur = cur.next;
                prve.next = cur;
            }
        }
        return head;
    }
    //2.BM13判断一个链表是否为回文结构(快慢指针+反转链表)
    public boolean isPail (ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode cur = slow.next;
        ListNode prve = null;
        ListNode tmp = null;
        while(cur != null) {
            tmp = cur.next;
            cur.next = prve;
            prve = cur;
            cur = tmp;
        }
        while(prve != null) {
            if(head.val != prve.val) {
                return false;
            }
            head = head.next;
            prve = prve.next;
        }
        return true;

    }
    //3.BM10两个链表的第一个公共节点
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        int len1 = 0;
        int len2 = 0;
        ListNode p1 = pHead1;
        ListNode p2 = pHead2;
        while (p1 != null) {
            p1 = p1.next;
            len1++;
        }
        while (p2 != null) {
            p2 = p2.next;
            len2++;
        }
        p1 = pHead1;
        p2 = pHead2;
        int len = len1 - len2;
        if (len < 0) {
            p1 = pHead2;
            p2 = pHead1;
            len = len2 - len1;
        }
        while (len != 0) {
            p1 = p1.next;
            len--;
        }
        while (p1 != null && p2 != null && p1 != p2) {
            p1 = p1.next;
            p2 = p2.next;
        }
        if (p1 == p2 && p1 == null) {
            return null;
        }
        return p1;
    }
}
